import { Config, clickConfig, viewConfig, pageViewConfig } from './types';
import { isUrlDebug } from './tools';

export const clickConf: clickConfig = {
  encryptType: 0,
  clickAttr: 'data-mc',
  clickType: 0,
  clickCount: 0,
};

export const viewConf: viewConfig = {
  encryptType: 0,
  viewAttr: 'data-mv',
  viewCount: 0,
  viewDuration: 0,
  threshold: 0,
  selectors: true,
  root: null,
};

export const pageViewConf: pageViewConfig = {
  pageViewDuration: 0,
};


const conf: Config = {
  ...clickConf,
  ...viewConf,
  ...pageViewConf,
  debug: false,
};

export function setConfig(options: Config): Config {
  const urlParam: any = { };
  if (isUrlDebug()) urlParam.debug = true;
  return Object.assign(conf, options, urlParam);
}

function setBaseConfig(options: any, config: clickConfig | viewConfig | pageViewConfig) {
  const { encryptType } = options;
  if (typeof encryptType === 'number' && encryptType >= 0 && encryptType <= 2) conf.encryptType = encryptType;
  return Object.assign(config, options);
}

export function setClickConfig(options: clickConfig): clickConfig {
  return setBaseConfig(options, clickConf);
}

export function setViewConfig(options: viewConfig): viewConfig {
  return setBaseConfig(options, viewConf);
}

export function setPageViewConfig(options: pageViewConfig): pageViewConfig {
  return setBaseConfig(options, pageViewConf);
}

export default conf;
